package cn.b.sky.mes.mm.service.impl;

import java.util.List;

import cn.b.sky.mes.mm.dao.MoveTypeDao;
import cn.b.sky.mes.mm.model.MoveType;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.mm.model.condition.MoveTypeCondition;
import cn.b.sky.mes.mm.service.MoveTypeService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service
public class MoveTypeServiceImpl implements MoveTypeService {

  private static final Logger LOG = LoggerFactory.getLogger(MoveTypeServiceImpl.class);

  @Autowired
  MoveTypeDao moveTypeDao;

  @Override
  public List<MoveType> selectMoveTypeListByCondition(MoveTypeCondition condition) {
    return moveTypeDao.selectMoveTypeListByCondition(condition);
  }

  @Override
  public int selectMoveTypeCountByCondition(MoveTypeCondition condition) {
    return moveTypeDao.selectMoveTypeCountByCondition(condition);
  }

  @Override
  public MoveType getMoveType(String siteCode, String moveTypeCode) {
    return moveTypeDao.getMoveType(siteCode, moveTypeCode);
  }

  @Override
  public void insertMoveType(MoveType moveType) {
    moveTypeDao.insertMoveType(moveType);
  }

  @Override
  public void updateMoveType(MoveType moveType) {
    moveTypeDao.updateMoveType(moveType);
  }

  @Override
  public BatchJobReport deleteMoveType(String siteCode, String moveTypeCodeCode) {
    if (StringUtils.isEmpty(siteCode) || StringUtils.isEmpty(moveTypeCodeCode)) {
      return new BatchJobReport("", "siteCode or defectTypeCode is null", false, SecurityContextUtil.getLanguageCode(), SecurityContextUtil.getSiteCode());
    }
    String message = moveTypeCodeCode;
    boolean success = true;
    String cause = "";

    try {
      moveTypeDao.deleteMoveType(siteCode, moveTypeCodeCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError( e.getMessage())) {
        //外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success, SecurityContextUtil.getLanguageCode(), SecurityContextUtil.getSiteCode());
  }

  @Override
  public List<MoveType> selectAllMoveTypeList(MoveTypeCondition condition) {
    if (StringUtils.isBlank(condition.getSiteCode())) {
      return null;
    }
    return moveTypeDao.selectAllMoveTypeList(condition);
  }

  @Override
  public List<MoveType> selectMoveTypeSuggests(MoveTypeCondition condition) {
    return moveTypeDao.selectMoveTypeSuggests(condition);
  }

}
